<?php
		
	/* SVN FILE: $Id: sendmail.php 8 2011-03-13 08:32:38Z michele.andreoletti@gmail.com $ */
	
	/**
	 * Project Name : arcadia
	 *
	 * @author $Author: michele.andreoletti@gmail.com $
	 * @version $Revision: 8 $
	 * @lastrevision $Date: 2011-03-13 08:32:38 +0000 (Sun, 13 Mar 2011) $
	 * @filesource $URL: http://arcadia.googlecode.com/svn/trunk/include/sendmail.php $
	 */
	
	// sendmail.php E' UTILIZZATO PER INVIO E-MAIL TRAMITE BOTTONE NELLA PAGINA 'view.php'
	
	if (!isset($sAPP) || !is_string($sAPP) || !isset($aSUITE) || !is_array($aSUITE) || !isset($aSUITE[$sAPP])) {

	   	/**
		 * Intercetto la situazione in cui viene eseguito direttamente questo script (ovvero quando
		 * questo script non e' incluso in $sAPP/sendmail.php
		 *
		 */
		
		fnUpdateLog('APP NOT DEFINED', 0, '', 'E');
		header('Location: ../index.php?sMsg=app-not-defined');
		die();
	} 
	
	include('Mail.php');
	include('Mail/mime.php');

	fnGetValueFromGET(array('sAction','sField','sEmailTemplate','sRecipient','sCarbonCopy','iView','sViewItemLink'));
	
	$sScript = fnCurrentScript();


	$sTo = '';
	$sCc = '';
	$crlf = "\n";
	$html = $sSubject = '';	
		
	/**
	 * From
	 */
	 
	$sFrom = "Ingegneria Clinica <bioingegneria@ospedaliriuniti.bergamo.it>";	
	
	$sQuery = "SELECT * FROM `users` WHERE `sType`='user' AND `sUsername`='".fnReadSessionValue('sUsername')."'";
	$rResult = custom_mysql_query($sQuery);
	if (mysql_num_rows($rResult) == 1) {
		$aData = mysql_fetch_array($rResult);
		if (isset($aData['sEmail']) &&  is_valid_email($aData['sEmail'])) {
			$sFrom = (isset($aData['sDisplayname']) && !empty($aData['sDisplayname']) ? $aData['sDisplayname'] : $aData['sUsername'])." <".$aData['sEmail'].">";
		}
	}
	
	fnViewConst($sViewItemLink);
	
	// Array $aFieldsDataType	
	fnFieldsDataTypeArray($sViewSelectFrom);
	
	if (!empty($iView)) {

		$aData = fnCheckView($iView, $sViewSelectFrom, $sViewSelectGroupBy, 'aData');
		
		if (!empty($aData)) {
				
			$aData['sViewItemLink'] = $sViewItemLink; // mi serve questa assegnazione perchè verrà usata dalla funzione impress()
			$aData['sAPP'] = $sAPP; // mi serve questa assegnazione perchè verrà usata dalla funzione impress()
			$aData['sBASE_URL'] = $sSUITE_URL; // mi serve questa assegnazione perchè verrà usata dalla funzione impress()
			
			switch ($sAction) {
		
				case "send-email":
					
					/**
					 * Testo E-mail
					 */
				
					$aTemplate = fnMailTemplate($sEmailTemplate);
					
					$sHtml = utf8_decode($aTemplate['sHtml']);
					$sHtml = impress(ereg_replace("[\r\n]|$","</p>", $sHtml));
				
					$sSubject = utf8_decode($aTemplate['sSubject']);
					$sSubject = impress($sSubject);

					/**
					 * Destinatari 
					 */
					 
					$sTo = fnMailRecipients($sRecipient);
					$sCc = fnMailRecipients($sCarbonCopy);	
					
					$aHdrs = array(
						'From'		=> $sFrom,
						'To'		=> $sTo,		
						'Cc'		=> $sCc,
						'Subject'	=> $sSubject
					);
					
					$oMime = new Mail_mime($crlf);
					$oMime->setHTMLBody($sHtml);
					// do not ever try to call these lines in reverse order
					$sBody = $oMime->get();
					$aHdrs = $oMime->headers($aHdrs);
					$oMail =& Mail::factory('mail');
					$oMail->send($sTo, $aHdrs, $sBody);

					$sQuery = "UPDATE `$sViewSelectFrom` SET `$sField`=CURRENT_TIMESTAMP() WHERE `id`='$iView'";
					$rResult = custom_mysql_query($sQuery);
	
					$sQuery = "SELECT * FROM `$sViewSelectFrom` WHERE `id`='$iView'";
					$rResult = custom_mysql_query($sQuery);
					
					if (mysql_num_rows($rResult) == 1) {
						$aData = mysql_fetch_array($rResult);
						if (isset($aData[$sField]) && $aData[$sField] != "0000-00-00 00:00:00") {
							fnUpdateLog("EMAIL SENT: [".$sRecipient."]", $iView, $sViewSelectFrom, 'I');
							$sOutput = "Email a ".strip_tags($sTo).": ".fnHtmlTimestamp($aData[$sField]);
							$sOutput = fnJsonOutput($sScript, $sAction, $sOutput, 0);
							
						} else { 			
						fnUpdateLog("EMAIL ERROR", $iView, $sViewSelectFrom, 'E');
						$sOutput = fnJsonOutput($sScript, $sAction, '', 505);
						
						}
						
					} else { 			
						fnUpdateLog("EMAIL ERROR", $iView, $sViewSelectFrom, 'E');
						$sOutput = fnJsonOutput($sScript, $sAction, '', 505);
						
					}
					
					break;
				
				default:
					fnUpdateLog('ACTION NOT VALID', $iView, $sViewSelectFrom, 'E');
					$sOutput = fnJsonOutput($sScript, $sAction, '', 101);
					break;

			}  
			
		} else {
			fnUpdateLog('RECORD NOT FOUND', $iView, $sViewSelectFrom, 'E');
			$sOutput = fnJsonOutput($sScript, $sAction, '', 111);
			
		}
	
	} else {
		fnUpdateLog('RECORD ID EMPTY', $iView, $sViewSelectFrom, 'E');
		$sOutput = fnJsonOutput($sScript, $sAction, '', 100);

	}
	
	echo $sOutput;	
	die();
	
?>
